<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('基金名称')" prop="fundName">
            <!--            <el-input v-model="form.fundName" :placeholder="$t('基金名称')" show-word-limit maxlength="32" />-->
            <template>
              <router-link
                target="_blank"
                :to="{path:'/fund/fundBasehome',query:{id:form.fundId}}"
                class="link_a"
              >{{ form.fundName }}
              </router-link>
            </template>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('基金成立日')" prop="foundationDate">
            <el-date-picker
              v-model="form.foundationDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('基金成立日')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金到期日')" prop="fundMaturityDate">
            <el-date-picker
              v-model="form.fundMaturityDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('基金到期日')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('管理人')" prop="fundManager">

            <el-select v-model="form.fundManager" filterable clearable :placeholder="$t('请选择')">
              <el-option
                v-for="item in gpBaseOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                @change="Manager(item.id)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('基金目标募集规模') + $t('金额单位')" prop="targetAmount">
            <Money v-model="form.targetAmount" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('备案产品编码')" prop="productCode">
            <el-input v-model="form.productCode" :placeholder="$t('备案产品编码')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('其他内容补充')" prop="additionalContent">
            <el-input
              v-model="form.additionalContent"
              type="textarea"
              :placeholder="$t('其他内容补充')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable
        :list.sync="form.fileList"
        data-type="fund_record"
        :business-id="autoBusinessId($route.query.id, form)"
        :btn-show="!otherBtnHide"
      />
    </el-form>
  </div>
</template>

<script>
import { saveFundRecord, getFundRecord } from '@/api/fund/record/fundRecord'
import { getFundBase } from '@/api/fund/fundBase'
import { findGp } from '@/api/fund/gp/gpBase'

export default {
  data() {
    return {
      // 表单参数
      form: {
        id: undefined,
        fundId: this.$route.query.id,
        fundName: undefined,
        targetAmount: undefined,
        foundationDate: undefined,
        fundMaturityDate: undefined,
        fundManager: undefined,
        productCode: undefined,
        additionalContent: undefined
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        targetAmount: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        foundationDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        fundMaturityDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        fundManager: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        productCode: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      // 操作按钮默认不展示
      buttonShow: true,
      otherBtnHide: false,
      managerOpen: false,
      // GP管理人
      gpBaseOptions: []
    }
  },
  created() {
    this.$bus.$emit('async_done')
    // 查询GP管理人类型为GP
    this.findGpOption()
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getFundRecord(data.id).then(response => {
          this.form = response.data
          getFundBase(this.form.fundId).then(response => {
            this.form.fundName = response.data.fundFullName
            this.form.targetAmount = response.data.fundBaseFund.targetAmount
            this.form.foundationDate = response.data.foundationDate
            this.form.fundManager = response.data.fundBaseFund.managerId
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        if (data != null) {
          this.form.fileList = data.fileList
        }
        getFundBase(this.$route.query.id).then(response => {
          this.form.fundName = response.data.fundFullName
          this.form.targetAmount = response.data.fundBaseFund.targetAmount
          this.form.foundationDate = response.data.foundationDate
          this.form.fundManager = response.data.fundBaseFund.managerId
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: this.$route.query.id,
        fundName: undefined,
        targetAmount: undefined,
        foundationDate: undefined,
        fundMaturityDate: undefined,
        fundManager: undefined,
        productCode: undefined,
        additionalContent: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveFundRecord(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.loading = false
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            })
          } else {
            this.locationError(document)
            reject()
          }
        })
      })
    },
    findGpOption() {
      findGp().then(response => {
        this.gpBaseOptions = response.data

        console.log(this.gpBaseOptions)
      })
    },
    Manager(id) {
      this.form.fundManager = id
    }
  }
}
</script>
